package com.rrd.dw.mr.gzip.kafka;

import java.io.IOException;
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import com.rrd.dw.utils.MD5Util;
import com.ucredit.babel.common.model.Job;
import com.ucredit.babel.common.model.behavior.AppInfo;
import com.ucredit.babel.common.model.behavior.DeviceInfo;
import com.ucredit.babel.common.model.behavior.EventInfo;
import com.ucredit.babel.common.model.behavior.UnloginUserBehaviorRecord;
import com.ucredit.babel.common.model.behavior.UserBehaviorRecord;
import com.ucredit.babel.common.model.behavior.UserInfo;

@Deprecated
public class UnLoginUserBehaviorParse {
	public static final String CRT001 =  "\001";
	public static final Pattern CRT001_PATTERN =  Pattern.compile("\001");
	private  String user_id="";
	private String timestamstr="";
	private StringBuilder sb = new StringBuilder(500);
	private  Text val = new Text();
	private  Text key= new Text();
	
	/**
	 * 用户行为基础数据  UNLOGIN_USER_BEHAVIOR
	 * @param userBehavior
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public  void parseUserBehaviorBase(Context context,UnloginUserBehaviorRecord userBehavior) throws IOException, InterruptedException{
		Job job=userBehavior.getJob();
    	AppInfo app = userBehavior.getAppInfo();
    	DeviceInfo device = userBehavior.getDeviceInfo();
   
    	if(app==null){
    		app = new AppInfo();
    	}
    	if(device==null){
    		device=new DeviceInfo();
    	}
    	sb.append(device.getImei()).append(app.getAppBundle()).append(userBehavior.getUploadTime()).append(userBehavior.getTimestamp());
    	String id=MD5Util.getMd5(sb.toString());
    	sb.delete(0,sb.length());
    	sb.append(id).append(CRT001).append(job.getJobID()).append(CRT001);
    	//数据时间
    	sb.append(userBehavior.getTimestamp()==null?"":userBehavior.getTimestamp()).append(CRT001);
    	//应用唯一标识
    	sb.append(app.getAppBundle()==null?"":app.getAppBundle()).append(CRT001);
    	//应用版本
    	sb.append(app.getAppVersion()==null?"":app.getAppVersion()).append(CRT001);
    	//行为数据采集SDK版本
    	sb.append(app.getLibVersion()==null?"":app.getLibVersion()).append(CRT001);
    	//是否越狱
    	sb.append(device.getJailbroken()==null?"":device.getJailbroken()).append(CRT001);
    	//屏幕尺寸
    	sb.append(device.getScreen()==null?"":device.getScreen()).append(CRT001);
    	//是否为模拟器
    	sb.append(device.getSimulator()==null?"":device.getSimulator()).append(CRT001);
    	//网络类型
    	sb.append(device.getNetworkType()==null?"":device.getNetworkType()).append(CRT001);
    	//系统名称
    	sb.append(device.getOs()==null?"":device.getOs()).append(CRT001);
    	//是否为Wi-Fi
    	sb.append(device.getWifi()==null?"":device.getWifi()).append(CRT001);
    	//设备唯一标识
    	sb.append(device.getImei()==null?"":device.getImei()).append(CRT001);
    	//设备名称
    	sb.append(device.getDeviceName()==null?"":device.getDeviceName()).append(CRT001);
    	//系统语言
    	sb.append(device.getLanguage()==null?"":device.getLanguage()).append(CRT001);
    	//系统版本
    	sb.append(device.getOsVersion()==null?"":device.getOsVersion()).append(CRT001);
    	//设备型号
    	sb.append(device.getModel()==null?"":device.getModel()).append(CRT001);
    	//运营商
    	sb.append(device.getCarrier()==null?"":device.getCarrier()).append(CRT001);
    	//业务系统
    	sb.append(job.getSystemID()==null?"":job.getSystemID());
    	key.set("unbehaviorBase");
    	val.set(sb.toString().replace("\n", " "));
    	context.write(key, val);
    	parseUserBehaviorEvent(context,userBehavior,id);
	}
	
	/**
	 * 用户行为事件数据
	 * @param userBehavior
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public  void parseUserBehaviorEvent(Context context,UnloginUserBehaviorRecord userBehavior,String base_id) throws IOException, InterruptedException{
    	Job job = userBehavior.getJob();
    	DeviceInfo device = userBehavior.getDeviceInfo();
    	if(device==null){
    		device = new DeviceInfo();
    	}
    	Set<EventInfo> list =userBehavior.getEventsInfo();
    	for(EventInfo e :list){
    		
    		sb.delete(0, sb.length());
    		sb.append(base_id).append(CRT001);
    		sb.append(device.getImei()==null?"":device.getImei()).append(CRT001);
    		sb.append(job.getSystemID()==null?"":job.getSystemID()).append(CRT001);

    		//数据时间
    		sb.append(userBehavior.getTimestamp()).append(CRT001);
    		//事件通用唯一识别码
    		sb.append(e.getUuid()).append(CRT001);
    		//事件时间戳
    		sb.append(e.getTimestamp()).append(CRT001);
    		//事件内容 
    		sb.append(e.getContent()).append(CRT001);
    		//事件名称
    		sb.append(e.getEventName()).append(CRT001);
    		//事件类型
    		sb.append(e.getEventType()).append(CRT001);
    		//行为类型
    		sb.append(e.getActionType()).append(CRT001);
    		sb.append(e.getPageName()==null?"":e.getPageName()).append(CRT001);
    		sb.append(e.getFrom()==null?"":e.getFrom());
    		key.set("unbehaviorEvent");
        	val.set(sb.toString().replace("\n", " "));
        	context.write(key, val); 
    		
    	}
    	
	}
}
